﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Configuration;


namespace VirtualDrum.lib.db
{
	class Access : ControlDatabase
	{
		protected OleDbConnection _objConn;
		protected OleDbCommand _objCommand;
		protected OleDbDataReader _objDataReader;

		//rewrite
		protected Access()
			: base()
		{			
			this._sql = "";
			this._objCommand = null;
			this.getDBConnect();
		}

		//rewrite
		protected override void getDBConnect()
		{
			try{
				this._objConn = new OleDbConnection(this.getConnString());
				//LittleTools.debug("数据库链接成功！");
			}catch(OleDbException ex){
				//LittleTools.debug("数据库连接失败！");
				ex.GetBaseException();
			}
		}
		
		//rewrite
		protected override void getDbCommand(string sql)
		{
			this._objCommand = new OleDbCommand(sql, this._objConn);
			if(!this._objCommand.Connection.State.ToString().Equals("Open")){
				this._objCommand.Connection.Open();
			}			
			//LittleTools.debut(sql);
		}

		//rewrite
		public static Access getInstance()
		{
			if (Access.objCDB == null)
			{
				lock(obj){
					if (Access.objCDB == null)
					{
						Access.objCDB = new Access();
					}
				}
			}
			return (Access)Access.objCDB;
		}

		//rewrite
		public override IList<string []> executeQuery(string sql)
		{
			this.getDbCommand(sql);
			return this.makeResultArrayList();
		}

		//rewrite
		public override int executeNonQuery(string sql)
		{
			this.getDbCommand(sql);
			return this._objCommand.ExecuteNonQuery();
		}

		//rewrite
		public override void closeConnection()
		{
			this._objCommand.Connection.Close();
			this._objCommand = null;
		}

		//rewrite
		protected override IList<string []> makeResultArrayList()
		{
			this._objResultAL.Clear();
			this._objDataReader = this._objCommand.ExecuteReader();
			while (this._objDataReader.Read())
			{
				string[] objs = new string[this._objDataReader.FieldCount];
				for(int i = 0; i < objs.Length; i ++){
				    objs[i] = this._objDataReader.GetValue(i).ToString();
				}
				this._objResultAL.Add(objs);
			}
			this.closeConnection();
			this._objDataReader.Close();
			return this._objResultAL;
		}

		//rewrite
		protected override string getConnString()
		{
			return ConfigurationSettings.AppSettings["connectionString"];
		}

		//rewrite
		public override string getDateTime()
		{
			return DateTime.Now.ToString();
		}
	}
}
